/**
 * 
 */
package graph;

import spatial.MBR;
import spatial.SpatialSearchTreeEntry;
import spatial.SpatialSearchTreeObject;
import util.MathUtil;

/**
 * @author xiaohui
 * 
 */
public class GraphNode implements SpatialSearchTreeObject {
	private long id;
	/**
	 * x-coordinate.
	 */
	protected double x = 0;
	/**
	 * y-coordinate.
	 */
	protected double y = 0;

	/**
	 * Constructor.
	 * @param  id  id of the node
	 * @param  x  x-coordinate
	 * @param  y  y-coordinate
	 */
	public GraphNode(long id, double x, double y) {
		this.id = id;
		this.x = x;
		this.y = y;

	}

	@Override
	public MBR getMBR() {
		return new MBR(x, y, 0, 0);
	}

	@Override
	public int getNumOfDimensions() {
		return 2;
	}

	@Override
	public double computeDistanceTo(double[] point) {
		return MathUtil.computeDistance(x, y, point[0], point[1]);
	}

	@Override
	public boolean contains(MBR rect) {
		if (rect == null)
			return false;
		else
			return getMBR().contains(rect);
	}

	@Override
	public long getId() {
		return id;
	}

	@Override
	public boolean intersects(Object obj) {
		if (obj instanceof GraphNode) {
			return getMBR().intersects(((GraphNode) obj).getMBR());
		} else if (obj instanceof GraphEdge) {
			return getMBR().intersects(((GraphEdge) obj).getMBR());
		} else
			return false;
	}

	@Override
	public boolean intersects(MBR r) {
		return getMBR().intersects(r);
	}

	@Override
	public boolean isContainedBy(MBR rect) {
		return (rect.contains(getMBR()));
	}

	@Override
	public boolean isOfSameType(SpatialSearchTreeObject obj) {
		if (obj instanceof GraphNode)
			return true;
		else
			return false;
	}

	public String toString() { // Always a good idea for debuging
		// JUNG2 makes good use of these.
		return "GN" + id + " (" + x + "," + y + ")";
	}

}
